package com.kuang.controller;

import com.kuang.pojo.Books;
import com.kuang.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Niddles
 * @create 2021：05:23:13:01
 * @Theme default
 */
@Controller
@RequestMapping("/book")
public class BookController {
    @Autowired
    @Qualifier("bookService")
    private BookService bookService;

    @RequestMapping("/allBooks")
    public String list(Model model){
        List<Books> list = bookService.queryBooks();
        model.addAttribute("list",list);
        return "allBooks";
    }
    @RequestMapping("/addBook")
    public String addBook(Books books){
        System.out.println(books);
        bookService.addBook(books);
        return "redirect:/book/allBooks";
    }

    @RequestMapping("/updateBook")
    public String updateBook(Books books){
        System.out.println(books);
        int i = bookService.updateBook(books);
        if (i>0){
            System.out.println("更新成功"+books);
        }else{
            System.out.println("更新失败");
        }
        return "redirect:/book/allBooks";
    }

    @RequestMapping("/deleteBook/{bookId}")
    public String DeleteBook(@PathVariable("bookId") int id){
        bookService.deleteBook(id);
        return "redirect:/book/allBooks";
    }
    @RequestMapping("/queryBook")
    public String queryBook(String queryBookName,Model model){
        Books books = bookService.queryBookByName(queryBookName);
        List<Books> list = new ArrayList<Books>();
        list.add(books);
        model.addAttribute("list",list);
        return "allBooks";
    }



    @RequestMapping("/toaddBook")
    public String toAddBookPage(){
        return "addBook";
    }

    @RequestMapping("/toupdateBook")
    public String toUpdateBookPage(int id,Model model){
        Books book = bookService.queryBookById(id);
        model.addAttribute("qBook",book);
        return "updateBook";
    }



}
